package com.oneonefive.trainticketsystem.controller;

import com.oneonefive.trainticketsystem.pojo.Train;
import com.oneonefive.trainticketsystem.service.TrainService;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Timestamp;
import java.util.List;

@RestController
@CrossOrigin
public class TrainController {
    final
    TrainService trainService;

    public TrainController(TrainService trainService) {
        this.trainService = trainService;
    }

    @RequestMapping("/selectAllTrain")
    public List<Train> selectAllTrain(Integer trainId,String startLocation, String endLocation, Integer page){
        return trainService.selectAllTrain(trainId,startLocation,endLocation,(page-1)*10);
    }

    @RequestMapping("/getTrainRowCount")
    public int getRowCount(Integer trainId,String startLocation,String endLocation){
        return trainService.getRowCount(trainId,startLocation,endLocation);
    }
    //2023-03-01T04:00:00.000Z
    @RequestMapping("/saveOrUpdateTrain")
    public boolean saveOrUpdateTrain(Integer trainId, String startLocation, String endLocation, String departureTime, String arrivalTime){
        Timestamp t1=Timestamp.valueOf(departureTime.substring(0, 10) + " " + departureTime.substring(11, 19));
        Timestamp t2=Timestamp.valueOf(arrivalTime.substring(0, 10) + " " + arrivalTime.substring(11, 19));
        return trainService.saveOrUpdate(new Train(trainId,startLocation,endLocation,t1,t2));
    }

    @RequestMapping("/deleteTrainById")
    public boolean deleteTrainById(Integer trainId){
        return trainService.removeById(trainId);
    }
}
